package com.gxa.jjty.cjr.service.impl;

import com.gxa.jjty.cjr.mapper.HealthyMapper;
import com.gxa.jjty.cjr.mapper.ScoreMapper;
import com.gxa.jjty.cjr.service.HealthyService;
import com.gxa.jjty.core.common.ExceptionCode;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.exception.BusinessException;
import com.gxa.jjty.core.pojo.Healthy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @author : 宸濯
 * Date: 2021/12/06 14:52
 * Description:
 * Version: V1.0
 */
@Service("healthyService")
public class HealthyServiceImpl implements HealthyService {

    @Autowired(required = false)
    private HealthyMapper healthyMapper;

    @Autowired(required = false)
    private ScoreMapper scoreMapper;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    public static final String[] SCORES ={"heartRateList","bloodGlucoseList","bloodOxygenList","bloodFatList","bloodPressureList","foodList","recitalList","healthy:"};
    public static final String HEALTHY="healthy:";


    @Override
    public Map<String, Object> upsert(Healthy healthy) throws BusinessException {
        Map<String,List<Map<String,Object>>> scores = (Map<String, List<Map<String,Object>>>) redisTemplate.opsForValue().get("scores");
        if (scores==null){
            scores = new HashMap<>(16);
            List<Map<String,Object>> heartRates = scoreMapper.selectByName("心率");
            List<Map<String,Object>> bloodGlucoses = scoreMapper.selectByName("血糖");
            List<Map<String,Object>> bloodOxygens = scoreMapper.selectByName("血氧");
            List<Map<String,Object>> bloodFats = scoreMapper.selectByName("血脂");
            List<Map<String,Object>> bloodPressures = scoreMapper.selectByName("血压");
            List<Map<String,Object>> foods = scoreMapper.selectByName("膳食");
            List<Map<String,Object>> recitals = scoreMapper.selectAll();
            scores.put(SCORES[0], heartRates);
            scores.put(SCORES[1], bloodGlucoses);
            scores.put(SCORES[2], bloodOxygens);
            scores.put(SCORES[3], bloodFats);
            scores.put(SCORES[4], bloodPressures);
            scores.put(SCORES[5], foods);
            scores.put(SCORES[6], recitals);
            redisTemplate.opsForValue().set("scores",scores);
        }
        int scoreTotal=0;
        //设置心率表现
        healthy.setHeartRateExpre("数据超过阈值");
        for (Map<String, Object> scoreList : scores.get(SCORES[0])) {
            if (Double.parseDouble(scoreList.get("start").toString())<=healthy.getHeartRate()
                    && healthy.getHeartRate()<Double.parseDouble(scoreList.get("end").toString())){
                healthy.setHeartRateExpre(scoreList.get("expression").toString());
                scoreTotal+=Integer.parseInt(scoreList.get("value").toString());
            }
        }
        //设置血糖表现
        healthy.setBloodGlucoseExpre("数据超过阈值");
        for (Map<String, Object> scoreList : scores.get(SCORES[1])) {
            if (Double.parseDouble(scoreList.get("start").toString())<=healthy.getBloodGlucose()
                    && healthy.getBloodGlucose()<Double.parseDouble(scoreList.get("end").toString())){
                healthy.setBloodGlucoseExpre(scoreList.get("expression").toString());
                scoreTotal+=Integer.parseInt(scoreList.get("value").toString());
            }
        }
        //设置血氧表现
        healthy.setBloodOxygenExpre("数据超过阈值");
        for (Map<String, Object> scoreList : scores.get(SCORES[2])) {
            if (Double.parseDouble(scoreList.get("start").toString())<=healthy.getBloodOxygen()
                    && healthy.getBloodOxygen()<Double.parseDouble(scoreList.get("end").toString())){
                healthy.setBloodOxygenExpre(scoreList.get("expression").toString());
                scoreTotal+=Integer.parseInt(scoreList.get("value").toString());
            }
        }
        //设置血脂表现
        healthy.setBloodFatExpre("数据超过阈值");
        for (Map<String, Object> scoreList : scores.get(SCORES[3])) {
            if (Double.parseDouble(scoreList.get("start").toString())<=healthy.getBloodFat()
                    && healthy.getBloodFat()<Double.parseDouble(scoreList.get("end").toString())){
                healthy.setBloodFatExpre(scoreList.get("expression").toString());
                scoreTotal+=Integer.parseInt(scoreList.get("value").toString());
            }
        }
        //设置血压表现
        healthy.setBloodPressureExpre("数据超过阈值");
        for (Map<String, Object> scoreList : scores.get(SCORES[4])) {
            if (Double.parseDouble(scoreList.get("start").toString())<=healthy.getBloodPressure()
                    && healthy.getBloodPressure()<Double.parseDouble(scoreList.get("end").toString())){
                healthy.setBloodPressureExpre(scoreList.get("expression").toString());
                scoreTotal+=Integer.parseInt(scoreList.get("value").toString());
            }
        }
        //设置膳食表现
        healthy.setFoodExpre("数据超过阈值");
        for (Map<String, Object> scoreList : scores.get(SCORES[5])) {
            if (Double.parseDouble(scoreList.get("start").toString())<=healthy.getFood()
                    && healthy.getFood()<Double.parseDouble(scoreList.get("end").toString())){
                healthy.setFoodExpre(scoreList.get("expression").toString());
                scoreTotal+=Integer.parseInt(scoreList.get("value").toString());
            }
        }
        //设置总表现
        for (Map<String, Object> scoreList : scores.get(SCORES[6])) {
            if (Double.parseDouble(scoreList.get("start").toString())<=scoreTotal
                    && scoreTotal<Double.parseDouble(scoreList.get("end").toString())){
                healthy.setExpression(scoreList.get("expression").toString());
            }
        }
        healthy.setScore(scoreTotal);
        healthy.setLastUpdate(new Date());
        Map<String, Object> byId = healthyMapper.findById(healthy.getHandicappedId());
        JsonResult jsonResult = new JsonResult();
        if (byId!=null){
            healthy.setId(Long.valueOf(byId.get("id").toString()));
            int i = healthyMapper.updateByPrimaryKey(healthy);
            if (i!=1){
                throw new BusinessException(ExceptionCode.UPDATE_FALSE);
            }
            redisTemplate.opsForValue().set(HEALTHY+healthy.getHandicappedId(), healthy);
            jsonResult.setMsg("修改成功!");
        }else {
            int insert = healthyMapper.insert(healthy);
            if (insert!=1){
                throw new BusinessException(ExceptionCode.ADD_DEFEATED);
            }
            jsonResult.setMsg("添加成功!");
        }
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @Override
    public Map<String, Object> findById(Long id) throws BusinessException {
        Object o = redisTemplate.opsForValue().get(HEALTHY + id);
        if (o==null){
            Map<String, Object> healthy = healthyMapper.findById(id);
            if (healthy==null){
                throw new BusinessException(ExceptionCode.NODATA);
            }
            Date lastUpdate = (Date)healthy.get("lastUpdate");
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String format = simpleDateFormat.format(lastUpdate);
            healthy.put("lastUpdate",format);
            redisTemplate.opsForValue().set(HEALTHY+id, healthy);
            o=healthy;
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("查询成功!");
        jsonResult.setData(o);
        return jsonResult.getDataMap();
    }
}
